package com.tianqu.tms.www.financia.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.dto.TmsMarketCarFinancialDto;
import com.tianqu.tms.dto.TmsMarketCarFinancialParam;
import com.tianqu.tms.entity.CarImage;
import com.tianqu.tms.entity.TmsFinancial;
import com.tianqu.tms.www.financia.viewhelper.FinanciaCarInputViewHelper;

public class FinanciaCarCreationInputAction extends TmsAction {
	private Logger logger = Logger.getLogger(FinanciaCompanyCreationInputAction.class);
	private static final String IMGLIST = "imgList";// 图片list
	private static final String FLAG = "flag";

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response, UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		FinanciaCarInputViewHelper view = new FinanciaCarInputViewHelper();
		//edit
		String editId = (String)uc.getParameter("id");
		if(StringUtil.isNotBlank(editId)) {
			TmsFinancial financial = TmsFinancial.getInstance();
			TmsMarketCarFinancialParam param = new TmsMarketCarFinancialParam(); param.setId(editId);
			List<TmsMarketCarFinancialDto> list = financial.findTmsMarketCarFinancial(param);
			TmsMarketCarFinancialDto dto = new TmsMarketCarFinancialDto();
			if(list.size() > 0) dto = list.get(0);
			BeanUtils.copyProperties(view,dto ); 
			view.setIsEdit("1");
			request.setAttribute(TmsConstants.VIEWHELP, view);
			return mapping.findForward(TmsConstants.SUCCESS);
		}else {
			String flag = "";
			if (!StringUtil.isNullOrBlank((String) uc.getAttribute(FLAG))) {
				flag = (String) uc.getAttribute(FLAG);
			}
			if ("1".equals(flag)) {
				if (uc.getAttribute(TmsConstants.VIEWHELP) != null) {
					view = (FinanciaCarInputViewHelper) uc.getAttribute(TmsConstants.VIEWHELP);
				}

				List<CarImage> imgList = new ArrayList<CarImage>();
				if (uc.getAttribute(IMGLIST) != null) {
					imgList = (List<CarImage>) uc.getAttribute(IMGLIST);
				}
				if (imgList != null) {
					for (int i = 0; i < imgList.size(); i++) {
						CarImage entity = (CarImage) imgList.get(i);
						if (entity.getImgKey().equals(TmsConstants.ONE)) {
							view.setReportPhoto(entity.getImgPath());
						}
						if (entity.getImgKey().equals(TmsConstants.TWO)) {
							view.setMaterialImages1(entity.getImgPath());
						}
						if (entity.getImgKey().equals(TmsConstants.THREE)) {
							view.setMaterialImages2(entity.getImgPath());
						}
						if (entity.getImgKey().equals(TmsConstants.FOUR)) {
							view.setMaterialImages3(entity.getImgPath());
						}
					}
				} else {
					// 在list为空的情况下赋一个初值
					view.setReportPhoto(TmsConstants.BLANK);
					view.setMaterialImages1(TmsConstants.BLANK);
					view.setMaterialImages2(TmsConstants.BLANK);
					view.setMaterialImages3(TmsConstants.BLANK);
				}
			} else {
				uc.removeAttribute(IMGLIST);
				uc.removeAttribute(TmsConstants.VIEWHELP);
			}
			if (uc.getAttribute(FLAG) != null) {
				uc.removeAttribute(FLAG);
			}
		}
		
		
		request.setAttribute(TmsConstants.VIEWHELP, view);
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}

}
